<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>ZDH 部署日志</title>
    <meta name="keywords" content="ZDH 部署日志">
    <meta name="description" content="ZDH 部署日志">

    <link rel="shortcut icon" href="img/favicon.ico">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">

    <link href="css/plugins/chosen/chosen.css" rel="stylesheet">

    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <link href="css/plugins/datetimepicker/datetimepicker.css" rel="stylesheet">
    <link href="css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="row  border-bottom white-bg dashboard-header">


</div>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>部署日志状态查询</h5>
            <div class="ibox-tools">
                <a class="collapse-link">
                    <i class="fa fa-chevron-up"></i>
                </a>
            </div>
        </div>
        <div class="ibox-content">

            <form role="form" class="form-inline" id="server_logs_form">
                <div class="form-group">
                    <div class="input-group date form_datetime">
                        <input id="start_time" name="start_time" class="form-control" type="text" value=""
                               placeholder="开始时间" readonly>
                        <span class="add-on"><i class="icon-remove"></i></span>
                        <span class="add-on"><i class="icon-calendar"></i></span>
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group date form_datetime">
                        <input id="end_time" name="end_time" class="form-control" type="text" value=""
                               placeholder="结束时间" readonly>
                        <span class="add-on"><i class="icon-remove"></i></span>
                        <span class="add-on"><i class="icon-calendar"></i></span>
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group">
                        <select data-placeholder="状态" class="chosen-select" style="width:110px;" tabindex="2"
                                id="status" name="status">
                            <option value="">状态</option>
                            <option value="running" hassubinfo="true">运行中</option>
                            <option value="finish" hassubinfo="true">成功</option>
                            <option value="error" hassubinfo="true">失败</option>
                        </select>
                    </div>
                    <input id="search" οnsubmit='return false' type="button" class="btn btn-primary form-control"
                           style="margin-bottom:0px" value="查询"/>
                </div>

            </form>

            <div class="row row-lg">
                <div class="col-sm-12">
                    <!-- Example Events -->
                    <div class="example-wrap">
                        <div class="btn-group hidden-xs" id="exampleTableEventsToolbar" role="group">
                            <button id="remove" type="button" class="btn btn-outline btn-default">
                                <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                            </button>
                        </div>
                        <table id="exampleTableEvents" data-height="500" data-mobile-responsive="true">

                        </table>
                    </div>
                </div>
                <!-- End Example Events -->
            </div>
        </div>
    </div>

    </div>
</div>
<!-- 全局js -->
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>


<!-- 自定义js -->
<script src="js/content.js?v=1.0.0"></script>

<!-- Chosen -->
<script src="js/plugins/chosen/chosen.jquery.js"></script>


<script src="js/plugins/datetimepicker/bootstrap-datetimepicker.min.js"></script>

<script src="js/plugins/datetimepicker/bootstrap-datetimepicker.zh-CN.js"></script>

<!-- Bootstrap table -->
<script src="js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="js/plugins/layer/laydate/laydate.js"></script>
<!-- layer javascript -->
<script src="js/plugins/layer/layer.min.js"></script>

<script>
    $(document).ready(function () {

        var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
        var params = url.substr(1);//去掉问号
        var pa = params.split("&");
        var s = new Object();//定义一个全局变量-存储任务ID
        for (var i = 0; i < pa.length; i++) {
            s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
        }

        var height=400
        if($(document.body).height()*0.8>height){
            height=$(document.body).height()*0.8
        }
        $('#exampleTableEvents').attr("data-height",height)

        function openTabPage(url, title) {
            var wpd = $(window.parent.document);
            var mainContent = wpd.find('.J_mainContent');
            var thisIframe = mainContent.find("iframe[data-id='" + url + "']");
            var pageTabs = wpd.find('.J_menuTabs .page-tabs-content ')
            pageTabs.find(".J_menuTab.active").removeClass("active");
            mainContent.find("iframe").css("display", "none");
            if (thisIframe.length > 0) {	// 选项卡已打开
                thisIframe.css("display", "inline");
                pageTabs.find(".J_menuTab[data-id='" + url + "']").addClass("active");
            } else {
                var menuItem = wpd.find("a.J_menuItem[href='" + url + "']");
                var dataIndex = title == undefined ? menuItem.attr("data-index") : '9999';
                var _title = title == undefined ? menuItem.find('.nav-label').text() : title;
                var iframe = '<iframe class="J_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + url + '" frameborder="0" data-id="' + url
                    + '" seamless="" style="display: inline;"></iframe>';
                pageTabs.append(
                    ' <a href="javascript:;" class="J_menuTab active" data-id="' + url + '">' + _title + ' <i class="fa fa-times-circle"></i></a>');
                mainContent.append(iframe);
                //显示loading提示
                var loading = top.layer.load();
                mainContent.find('iframe:visible').load(function () {
                    //iframe加载完成后隐藏loading提示
                    top.layer.close(loading);
                });
            }

        }


        //日期范围限制
        var start = {
            elem: '#start_time',
            format: 'YYYY-MM-DD',
            min: '2000-01-01',
            max: '2099-06-16', //最大日期
            istime: true,
            istoday: false,
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };
        var end = {
            elem: '#end_time',
            format: 'YYYY-MM-DD',
            min: '2000-01-01',
            max: '2099-06-16',
            istime: true,
            istoday: false,
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        laydate(start);
        laydate(end);
        $("#start_time").val(getCurrentDate())
        $("#end_time").val(getCurrentDate())


        $('#status').chosen();

        function getCurrentDate() {
            var oDate = new Date(),
                oYear = oDate.getFullYear(),
                oMonth = oDate.getMonth() + 1,
                oDay = oDate.getDate(),
                oHour = oDate.getHours(),
                oMin = oDate.getMinutes(),
                oSen = oDate.getSeconds(),
                oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay);//最后拼接时间
            return oTime;
        };

        function getMyDate(str) {
            var oDate = new Date(str),
                oYear = oDate.getFullYear(),
                oMonth = oDate.getMonth() + 1,
                oDay = oDate.getDate(),
                oHour = oDate.getHours(),
                oMin = oDate.getMinutes(),
                oSen = oDate.getSeconds(),
                oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + " " + getzf(oHour) + ":" + getzf(oMin) + ":" + getzf(oSen);//最后拼接时间
            return oTime;
        };

        //补0操作
        function getzf(num) {
            if (parseInt(num) < 10) {
                num = '0' + num;
            }
            return num;
        }

        $('#search').click(function () {
            $('#exampleTableEvents').bootstrapTable('refresh', {
                url: "/build_server_log?" + $("#server_logs_form").serialize()+"&templete_id="+s.templete_id,
                contentType: "application/json;charset=utf-8",
                dataType: "json"
            });

        })

        $('#remove').click(function () {

            var rows = $("#exampleTableEvents").bootstrapTable('getSelections');// 获得要删除的数据
            if (rows.length == 0) {// rows 主要是为了判断是否选中，下面的else内容才是主要
                layer.msg("请先选择要删除的记录!");
                return;
            } else {
                layer.confirm('是否删除执行记录', {
                    btn: ['确定','取消'] //按钮
                }, function(index){
                    var ids = new Array();// 声明一个数组
                    $(rows).each(function () {// 通过获得别选中的来进行遍历
                        ids.push(this.id);// cid为获得到的整条数据中的一列
                    });
                    console.log(ids)
                    deleteMs(ids)
                    layer.close(layer.index);
                }, function(){

                });


            }

        })

        function deleteMs(ids) {
            $.ajax({
                url: "server_logs_delete",
                data: "ids=" + ids,
                type: "post",
                dataType: "json",
                success: function (data) {
                    console.info("success")
                    $('#exampleTableEvents').bootstrapTable('refresh', {
                        url: "/build_server_log?" + $("#server_logs_form").serialize()+"&templete_id="+s.templete_id,
                        contentType: "application/json;charset=utf-8",
                        dataType: "json"
                    });
                },
                error: function (data) {
                    console.info("error: " + data.responseText);
                }

            });
        }


        window.operateEvents = {
            'click #log_txt': function (e, value, row, index) {
                row.job_context
                window.open("log_txt.html?job_id=" + row.templete_id+"&task_log_id="+row.id + "&start_time=" + row.create_time + "&update_time=" + row.update_time);
                //openTabPage("log_txt.html?job_id=" + row.job_id+"&task_log_id="+row.id + "&start_time=" + row.start_time + "&update_time=" + row.update_time, row.job_context + "日志")
            }
        };


        $('#exampleTableEvents').bootstrapTable({
            url: "/build_server_log?" + $("#server_logs_form").serialize()+"&templete_id="+s.templete_id,
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            search: true,
            pagination: true,
            showRefresh: false,
            showToggle: true,
            showColumns: true,
            iconSize: 'outline',
            toolbar: '#exampleTableEventsToolbar',
            icons: {
                refresh: 'glyphicon-repeat',
                toggle: 'glyphicon-list-alt',
                columns: 'glyphicon-list'
            },
            columns: [{
                checkbox: true,
                field: 'state',
                sortable: true
            }, {
                field: 'id',
                title: 'ID',
                visible: true,
                align : "center",
                valign : "middle"
            }, {
                field: 'templete_id',
                title: '模板ID',
                sortable: false,
                align : "center",
                valign : "middle",
                visible: true
            }, {
                field: 'build_task',
                title: '任务组说明',
                align : "center",
                valign : "middle",
                sortable: true
            }, {
                field: 'git_url',
                title: 'git地址',
                align : "center",
                valign : "middle",
                sortable: true
            }, {
                field: 'build_branch',
                title: '分支名',
                sortable: true,
                align : "center",
                valign : "middle",
                visible: true
            }, {
                field: 'create_time',
                title: '任务触发时间',
                sortable: true,
                align : "center",
                valign : "middle",
                formatter: function (value, row, index) {
                    return getMyDate(value);
                }
            }, {
                field: 'update_time',
                title: '任务更新时间',
                sortable: true,
                align : "center",
                valign : "middle",
                formatter: function (value, row, index) {
                    return getMyDate(value);
                }
            }, {
                field: 'status',
                title: '部署状态',
                sortable: true,
                align : "center",
                valign : "middle",
                events: operateEvents,
                formatter: function (value, row, index) {
                    var context="已完成"
                    var cls="btn-primary"
                    if(value=="0"){
                        context="进行中"
                        cls="btn-primary"
                    }
                    if(value=="2"){
                        context="失败"
                        cls="btn-danger"
                    }
                    return [
                        '<div style="text-align:center" >'+
                        '<div class="btn-group">'+
                        '<button type="button" id="build_log" class="btn '+cls+' btn-xs">'+context+'</button>'+
                        '<button type="button" id="log_txt" class="btn btn-warning btn-xs">日志</button>'+
                        '</div>'+
                        '</div>'
                    ].join('');
                }
            }
            ]
        });

    });
</script>
</body>

</html>